package top.api.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.logging.log4j.util.Strings;
import top.api.common.ResUlt;
import top.api.domain.dto.PageDto;
import top.api.domain.pojo.Class;
import top.api.domain.pojo.Student;
import top.api.domain.pojo.User;
import top.api.domain.vo.PageVo;
import top.api.service.ClassService;
import top.api.mapper.ClassMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author 86191
* @description 针对表【class】的数据库操作Service实现
* @createDate 2023-12-12 10:54:21
*/
@Service
@RequiredArgsConstructor
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class>
    implements ClassService{

    private  final ClassMapper classMapper;
    @Override
    public ResUlt<?> listPage(PageDto pageDto, Integer id, String classname) {
        IPage<Class> page = new Page(pageDto.getPageNum(),pageDto.getPageSize());
        LambdaQueryWrapper<Class> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(classname),Class::getClassname,classname);
        lqw.eq(ObjectUtils.isNotEmpty(id),Class::getId,id);
        classMapper.selectPage(page,lqw);
        PageVo<Class> pagevo = new PageVo<>(page.getRecords(),page.getTotal());
        return ResUlt.success(pagevo);
    }

    @Override
    public ResUlt<?> add(Class c) {
        if(Objects.isNull(classMapper.selectOne(new LambdaQueryWrapper<Class>().eq(Class::getClassname,c.getClassname())))){
            classMapper.insert(c);
            return ResUlt.success();
        }
        return ResUlt.error("该班级已存在哦");
    }

    @Override
    public ResUlt<?> getOne(Integer id) {
        LambdaQueryWrapper<Class> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtils.isNotEmpty(id),Class::getId,id);
        Class aClass = classMapper.selectOne(lqw);
        return ResUlt.success(aClass);
    }

    @Override
    public ResUlt<?> delete(List id) {
        int ids = classMapper.deleteBatchIds(id);
        return ResUlt.success(ids);
    }

    @Override
    public ResUlt<?> changeClass(Class c) {
        LambdaQueryWrapper<Class> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtils.isNotEmpty(c.getId()),Class::getId,c.getId());
        int update = classMapper.update(c, lqw);
        if(update>0){
            return  ResUlt.success();
        }
        return  ResUlt.error();
    }

    @Override
    public ResUlt<?> changeStatus(Class c) {
        String loginId = StpUtil.getLoginId().toString();
        if("1".equals(loginId)){
            LambdaQueryWrapper<Class> lqw = new LambdaQueryWrapper<>();
            lqw.eq(ObjectUtils.isNotEmpty(c.getId()),Class::getId,c.getId());
            int update = classMapper.update(c, lqw);
            if(update>0){
                return ResUlt.success("更改成功！");
            }
        }
        return ResUlt.error("该账户不是管理员，不可以解禁操作哦");
    }
}




